import { batchUpdateRemarkApi } from "@/api/waybillManagement/businessWaybill";
import FormHook from "@/hook/formHook";
import { GeneraDataType } from "@/interface";
import { message } from "ant-design-vue";
export default function OperationHook(state: GeneraDataType) {
  const { formState, formDataRef, emit, passData, loading, type } = state;
  const { resetFields, validateZhInputHandle } = FormHook({ formState });

  const submitHandle = () => {
    formDataRef.value.validate().then(() => {
      const { remark, innerRemark } = formState;
      const params: any = {
        idList: passData.value.map((t: any) => t.shipmentId),
        type: formState.type
      };
      if (type.value === 1) params.remark = remark;
      else params.innerRemark = innerRemark;
      loading.value = true;
      batchUpdateRemarkApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200 && passData.value.id) {
            message.success("操作成功!");
            emit("addChargeSubmit");
          } else {
            message.success("操作成功!");
            emit("submit");
          }
        })
        .finally(() => (loading.value = false));
    });
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  return {
    onFinishFailed,
    resetHandle,
    resetFields,
    validateZhInputHandle,
    submitHandle
  };
}
